<template>
  <div class="page">
    page
  </div>
</template>
<script type="text/javascript">
export default {
  data() {
    return {
    }
  },
   created() {
                this.getUrl()
            },
getUrl() {
          let userAgent = navigator.userAgent;
          if (userAgent.includes("iPhone") || userAgent.includes("iPad")) {
              sessionStorage.setItem("originUrl", location.href); // 用于ios分享
          }
          this.getBaseInfos();
      },

      // 编码函数
      getUrlParam(name) {
          var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
          var r = window.location.search.substr(1).match(reg); //匹配目标参数

          if (r != null) return unescape(r[2]);
          return null; //返回参数值
      },

      getBaseInfos() {
          if (this.isWeiXin()) {
              const code = this.getUrlParam("code"); // 截取路径中的code
              if (code == null || code === "") {
                  let url = "";
                  let userAgent = navigator.userAgent;
                  if (userAgent.includes("iPhone") || userAgent.includes("iPad")) {
                      url = sessionStorage.getItem("originUrl");
                  } else {
                      url = window.location.href;
                  }
                  window.location.href =
                      "https://open.weixin.qq.com/connect/oauth2/authorize?appid=你申请的appkey&redirect_uri=" +
                      encodeURIComponent(url) +
                      "&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect";
              } else {
              }

              if (code != "" && code != null) {
                  this.wxCode = code;
                  // this.getOpenid(code)
              }
          } else {
          }
      },

      isWeiXin() {
          var ua = window.navigator.userAgent.toLowerCase();
          if (ua.match(/MicroMessenger/i) == "micromessenger") {
              return true;
          } else {
              return false;
          }
      },

}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
